| API Documentation |  |

- MAIN PAGE
- NAMESPACES
- CLASSES

Public Member Functions | List of all members
nkScripts::Function Class Reference

Holds information, and controls, over a function in a scripting environment. More...

## Public Member Functions
OutputValue | defaultFunction (const DataStack &stack)
| The default place holder function. Set by default.
| Function (nkMemory::StringView name) noexcept
virtual | ~Function ()
nkMemory::StringView | getName () const
void | addParameter (FUNCTION_PARAMETER_TYPE type, nkMemory::StringView userTypeName="")
void | setFunction (FunctionCallback function)
void | reset ()

## Detailed Description

Holds information, and controls, over a function in a scripting environment.

Usage is to declare the function in the environment, sets its callback and parameters. Whenever a script will call the function, it will check and fill with parameter the stack and call the callback. Callback can then return whatever it needs after processing, and this will be used within the environment.

Here is a high level overview of the usage, first creation and setup :

Function* func = environment->setFunction("add") ;
func->setCallback(addCallback) ;
func->addParameter(FUNCTION_PARAMETER_TYPE::INT) ;
func->addParameter(FUNCTION_PARAMETER_TYPE::STRING) ;

Callable from lua as :

local x = add(5, "p") ;

That will execute this function :

OutputValue addCallback (const DataStack& stack)
{
int param0 = stack[0]._valInt ;
const std::string param1 = stack[1]._valString ;
return OutputValue(param1 + std::to_string(param0)) ;
}

And result in :

print(x) ;
> p5

## Constructor & Destructor Documentation

## ◆ Function()

nkScripts::Function::Function | ( | nkMemory::StringView | name | ) |
| noexcept

Constructor.

Parameters

  name | The name of the function.

## ◆ ~Function()

virtual nkScripts::Function::~Function | ( |  | ) |
| virtual

Destructor.

## Member Function Documentation

## ◆ getName()

nkMemory::StringView nkScripts::Function::getName | ( |  | ) | const

Returns
  The name of the function.

## ◆ addParameter()

void nkScripts::Function::addParameter | ( | FUNCTION_PARAMETER_TYPE | type,
|  | nkMemory::StringView | userTypeName = `""`
| ) |  |

Adds a parameter in the function signature. Parameters are expected in the order in which they are added within the Function. For instance, declaring :

func->addParameter(FUNCTION_PARAMETER_TYPE::INT) ;
func->addParameter(FUNCTION_PARAMETER_TYPE::STRING) ;

Will cause the function to need to be called with two parameters, an int as the first one, and a string as the second one. Failure to do so will trigger an error.

As a result, parameters need to be declared through this method to make the runtime checks and data stack fill possible.

Parameters

  type | The parameter type.
  userTypeName | If user data, the type awaited.

## ◆ setFunction()

void nkScripts::Function::setFunction | ( | FunctionCallback | function | ) |

Sets the callback function when this function is called in script.

Parameters

  function | The function to call.

## ◆ reset()

void nkScripts::Function::reset | ( |  | ) |

Resets and starts back from an empty function.

The documentation for this class was generated from the following file:

- Documentation/Headers/NilkinsScripts/Environments/Functions/Function.h